﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.ServiceModel;
using System.Text;
using Arch.Messages;
using Arch.Model;

namespace Arch.Services.ErrorHandling
{
    public class ValidationExceptionToFaultHandler : IExceptionToFaultHandler
    {
        public bool Recognize(Exception ex)
        {
            return ex is ValidationException;
        }

        public FaultException Convert(Exception ex)
        {
            var validationEx = ex as ValidationException;
            if(validationEx == null) throw new ArgumentException("ex");

            return new FaultException<ValidationMessage>(
                new ValidationMessage(validationEx.ValidationResult, validationEx.ValidationAttribute,
                                      validationEx.Value));
        }

        public IEnumerable<Type> HandledContracts
        {
            get { return new[] { typeof(ValidationMessage) }; }
        }
    }
}
